home *** CD-ROM | disk | FTP | other *** search
- #include "AIFFType.h"
- #include "SFConvert.h"
- #include <unix.h>
- #include <math.h>
- #include <SANE.h>
- #include "SDtype.h"
-
- extern void DoOSErrorAlert(Str255, Str255);
- extern void PstringCopy(char *, char *);
- extern void PstringCat(char *, char *);
- extern void DoOSErrorAlert(Str255, Str255);
-
- Boolean AIFFToSD(void);
- Boolean AIFFToINT16(void);
- Boolean AIFFToFloat(void);
-
-
- Str255 theMess;
-
- extern CursHandle watchCurs;
- extern int SoundFileType;
- extern int SFOUTPUTtype;
- extern int nrec;
-
- extern long TotalSamps;
- extern long SampleRate;
- extern long NumChannels;
- extern double MaxSample;
- extern double MinSample;
- extern long fileSize;
- extern ioParam myIOParmBlk;
- extern ioParam NewParmBlk;
- extern Boolean SDnoResource;
- extern OSErr theErr;
- extern long RecLength;
-
- Boolean AIFFToSD()
- {
- return(FALSE);
- }
- Boolean AIFFToINT16()
- {
- return(FALSE);
- }
- Boolean AIFFToFloat()
- {
- register long i;
- register double scalefactor;
- register double x;
- double peak;
- int *theIbuf, *Iptr;
- float *sp, *SampBuf;
- long nBytes;
- long nSamps;
- long bytesLeft;
- long sampBufsz;
- Chunk FormChunkHeader, *FormChunkHeaderPtr;
- CommonChunk CommonHeader, *CommonHeaderPtr;
- SoundDataChunk SoundDataHeader, *SoundDataHeaderPtr;
- BasicChunk BaseChunk, *BaseChunkPtr;
- long aLong, *aLongPtr;
- Boolean found;
- Fixed aFix;
- decform mydecform;
- Str255 myStr255;
-
-
- FormChunkHeaderPtr = &FormChunkHeader;
- CommonHeaderPtr = &CommonHeader;
- SoundDataHeaderPtr = &SoundDataHeader;
-
-
- myIOParmBlk.ioCompletion = 0L;
- myIOParmBlk.ioPosMode = fsFromStart;
- myIOParmBlk.ioPosOffset = 0L;
- theErr = PBSetFPos(&myIOParmBlk, FALSE);
-
-
-
- myIOParmBlk.ioBuffer = (Ptr)FormChunkHeaderPtr;
- myIOParmBlk.ioReqCount = sizeof(FormChunkHeader);
- theErr = PBRead(&myIOParmBlk, FALSE);
- if ( FormChunkHeader.ckID != 'FORM' ) {
- PstringCopy((char *)theMess, "\pError reading AIFF file, FORM header");
- DoOSErrorAlert(theMess, NIL);
- return(FALSE);
- }
-
-
- BaseChunkPtr = &BaseChunk;
- found = FALSE;
-
- while ( !found ) {
- myIOParmBlk.ioBuffer = (Ptr)BaseChunkPtr;
- myIOParmBlk.ioReqCount = sizeof(BaseChunk);
- theErr = PBRead(&myIOParmBlk, FALSE);
- if ( theErr == eofErr ) {
- DoOSErrorAlert("\pError: EOF reading reading AIFF file, no COMM header", NIL);
- return(FALSE);
- }
- if ( BaseChunk.ckID == 'COMM' ) {
- found = TRUE;
- myIOParmBlk.ioPosMode = fsFromMark;
- myIOParmBlk.ioPosOffset = -myIOParmBlk.ioActCount;
- theErr = PBSetFPos(&myIOParmBlk, FALSE);
- myIOParmBlk.ioPosMode = fsAtMark;
- /* get the common chunk info */
- myIOParmBlk.ioBuffer = (Ptr)CommonHeaderPtr;
- myIOParmBlk.ioReqCount = sizeof(CommonHeader);
- theErr = PBRead(&myIOParmBlk, FALSE);
-
- aFix = X2Fix( &CommonHeader.sampleRate);
- SampleRate = (long) aFix;
- mydecform.style = 1;
- mydecform.digits = -1;
- num2str(&mydecform, CommonHeader.sampleRate, &myStr255);
- StringToNum(myStr255, &SampleRate );
-
-
-
-
- }
- else { /* skip this chunk */
- myIOParmBlk.ioPosMode = fsFromMark;
- myIOParmBlk.ioPosOffset = BaseChunk.ckSize;
- theErr = PBSetFPos(&myIOParmBlk, FALSE);
- if ( theErr == eofErr ) {
- DoOSErrorAlert("\pError: EOF reading reading AIFF file, no COMM header", NIL);
- return(FALSE);
- }
- myIOParmBlk.ioPosMode = fsAtMark;
- }
-
- };
-
- /* rewind to start of FORM */
- myIOParmBlk.ioPosMode = fsFromStart;
- myIOParmBlk.ioPosOffset = sizeof(FormChunkHeader);
- theErr = PBSetFPos(&myIOParmBlk, FALSE);
- if ( theErr != noErr ) {
- DoOSErrorAlert("\pError re-positioning file to start of FORM", NIL);
- return(FALSE);
- }
-
- /* now look for SSND Chunk */
- found = FALSE;
-
- while ( !found ) {
- myIOParmBlk.ioBuffer = (Ptr)BaseChunkPtr;
- myIOParmBlk.ioReqCount = sizeof(BaseChunk);
- theErr = PBRead(&myIOParmBlk, FALSE);
- if ( theErr == eofErr ) {
- DoOSErrorAlert("\pError: EOF reading reading AIFF file, no SSND header", NIL);
- return(FALSE);
- }
- if ( BaseChunk.ckID == 'SSND' ) {
- found = TRUE;
- myIOParmBlk.ioPosMode = fsFromMark;
- myIOParmBlk.ioPosOffset = -myIOParmBlk.ioActCount;
- theErr = PBSetFPos(&myIOParmBlk, FALSE);
- myIOParmBlk.ioPosMode = fsAtMark;
- /* get the common chunk info */
- myIOParmBlk.ioBuffer = (Ptr)SoundDataHeaderPtr;
- myIOParmBlk.ioReqCount = sizeof(SoundDataHeader);
- theErr = PBRead(&myIOParmBlk, FALSE);
- }
- else { /* skip this chunk */
- myIOParmBlk.ioPosMode = fsFromMark;
- myIOParmBlk.ioPosOffset = BaseChunk.ckSize;
- theErr = PBSetFPos(&myIOParmBlk, FALSE);
- if ( theErr == eofErr ) {
- DoOSErrorAlert("\pError reading AIFF file, no SSND header", NIL);
- return(FALSE);
- }
- myIOParmBlk.ioPosMode = fsAtMark;
- }
-
- };
-
-
- /*
- *
- *
- *
- * SoundDataHeader.ckID = BaseChunk.ckID;
- SoundDataHeader.ckSize = BaseChunk.ckSize;
- aLongPtr = &aLong;
- myIOParmBlk.ioBuffer = (Ptr)aLongPtr;
- myIOParmBlk.ioReqCount = sizeof(aLong);
- theErr = PBRead(&myIOParmBlk, FALSE);
- SoundDataHeader.offset = aLong;
-
- myIOParmBlk.ioBuffer = (Ptr)aLongPtr;
- myIOParmBlk.ioReqCount = sizeof(aLong);
- theErr = PBRead(&myIOParmBlk, FALSE);
- SoundDataHeader.blockSize = aLong;
- *
- *
- */
-
-
-
- /* we only get here if we have found FORM, COMM and SSND chunks */
- RecLength = (long)16384;
- nrec = 0;
- SetProgressDialog();
-
-
- scalefactor = 2.0/SAMPMAX;
- MinSample = 0.0;
- MaxSample = 0.0;
- sampBufsz = RecLength / sizeof(int);
- theIbuf = (int *)NewPtr(sizeof(int) * sampBufsz);
- if ( (theErr = MemError()) != noErr ) {
- DoOSErrorAlert("\pcan't get enough memory for theIbuf", NIL);
- return(FALSE);
- }
- SampBuf = (float *)NewPtr( sizeof(float) * sampBufsz);
- if ( (theErr = MemError()) != noErr ) {
- DoOSErrorAlert("\pcan't get enough memory for SampBuf", NIL);
- return(FALSE);
- }
-
-
-
- bytesLeft = SoundDataHeader.ckSize - sizeof(SoundDataHeader.offset)
- - sizeof(SoundDataHeader.blockSize);
-
- myIOParmBlk.ioPosMode = fsAtMark;
- myIOParmBlk.ioPosOffset = NIL;
-
- while ( bytesLeft > 0L ) {
- if ( bytesLeft > RecLength )
- nBytes = RecLength;
- else
- nBytes = bytesLeft;
-
- myIOParmBlk.ioBuffer = (Ptr)theIbuf;
- myIOParmBlk.ioReqCount = nBytes;
- theErr = PBRead(&myIOParmBlk, FALSE);
- if (theErr != noErr ) {
- if ( theErr == eofErr )
- DoOSErrorAlert("\pEnd of file reached", NIL);
- }
- nBytes = myIOParmBlk.ioActCount;
- if ( nBytes != myIOParmBlk.ioReqCount ) {
- DoOSErrorAlert("\pRead less bytes than expected", NIL);
- if ( nBytes == 0L ) {
- DisposPtr((Ptr)SampBuf);
- DisposPtr((Ptr)theIbuf);
- DisposeProgDialog();
- return(FALSE);
- }
- }
- nSamps = nBytes / sizeof(int);
- for ( i = 0, Iptr = theIbuf, sp = SampBuf; i < nSamps; i++ ) {
- *sp = (*Iptr++ * scalefactor);
- if ( *sp < MinSample )
- MinSample = *sp;
- else if ( *sp > MaxSample )
- MaxSample = *sp;
- sp++;
- }
-
- /* write it out */
- NewParmBlk.ioReqCount = (long)(nSamps * sizeof(float));
- NewParmBlk.ioBuffer = (Ptr)SampBuf;
- if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
- DoOSErrorAlert("\pError writing to sample file", NIL);
- }
- if ( NewParmBlk.ioActCount != (long)(nSamps * sizeof(float)) ) {
- DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
- }
-
- nrec++;
- if ( !UpdateProgressDialog() ) {
- DisposPtr((Ptr)SampBuf);
- DisposPtr((Ptr)theIbuf);
- DisposeProgDialog();
- InitCursor();
- return(FALSE);
- }
- bytesLeft -= nBytes;
- }
-
- DisposPtr((Ptr)SampBuf);
- DisposPtr((Ptr)theIbuf);
- DisposeProgDialog();
- InitCursor();
-
-
- /* if ( FormChunkHeader.ckSize > SoundDataHeader.ckSize) {
- BaseChunkPtr = &BaseChunk;
- myIOParmBlk.ioBuffer = (Ptr)BaseChunkPtr;
- myIOParmBlk.ioReqCount = sizeof(BaseChunk);
- theErr = PBRead(&myIOParmBlk, FALSE);
- if ( BaseChunk.ckID == 'COMM' ) {
- myIOParmBlk.ioPosMode = fsFromMark;
- myIOParmBlk.ioPosOffset = -myIOParmBlk.ioActCount;
- theErr = PBSetFPos(&myIOParmBlk, FALSE);
-
- myIOParmBlk.ioPosMode = fsAtMark;
-
- myIOParmBlk.ioBuffer = (Ptr)CommonHeaderPtr;
- myIOParmBlk.ioReqCount = sizeof(CommonHeader);
- theErr = PBRead(&myIOParmBlk, FALSE);
-
- }
- }
- */
-
-
-
-
- /* be nosy and look at what else is in the file */
- /* found = FALSE;*/
-
- while ( !found ) {
- myIOParmBlk.ioBuffer = (Ptr)BaseChunkPtr;
- myIOParmBlk.ioReqCount = sizeof(BaseChunk);
- theErr = PBRead(&myIOParmBlk, FALSE);
- if ( theErr == eofErr ) {
- DoOSErrorAlert("\pError reading AIFF file, no SSND header", NIL);
- return(FALSE);
- }
- /* skip this chunk */
- myIOParmBlk.ioPosMode = fsFromMark;
- myIOParmBlk.ioPosOffset = BaseChunk.ckSize;
- theErr = PBSetFPos(&myIOParmBlk, FALSE);
- if ( theErr == eofErr ) {
- DoOSErrorAlert("\pError reading AIFF file, no SSND header", NIL);
- return(FALSE);
- }
- myIOParmBlk.ioPosMode = fsAtMark;
- };
-
- return(TRUE);
- }
-
-
-
-
-